#include "DoxyClass.h"
#include "DoxyDescription.h"
#include <QtXml>

#include"DoxMemberDef.h"



DoxyClass::DoxyClass(QString RefId)
{
    int pos  = RefId.lastIndexOf("/");
    m_refId = RefId.mid(pos+1);
     m_xmlFileName = RefId.append(".xml");
}

/**
 * @brief DoxyClass::loadFile Loads the file in and processes all of the internal structures to populate the data of the class
 * @return
 */
bool DoxyClass::loadFile()
{
    bool fileLoaded = false;

    QFile* file = new QFile(m_xmlFileName);

    if(file->open(QIODevice::ReadOnly))
    {

        QDomDocument document;
        if(document.setContent(file))
        {
            file->close();

            QDomElement docElem = document.documentElement();

            QDomNode root = docElem.firstChild();

            while(!root.isNull())
            {
                QDomElement element  = root.toElement();

                if(!element.isNull())
                {
                    qDebug() << element.tagName();
                    //double check that we have the correct file
                    if(element.attribute("id") == m_refId)
                    {
                        fileLoaded = true;

                        QDomNodeList list = element.childNodes();

                        for(int i =0; i < list.size(); i++)
                        {
                            if(!list.at(i).isNull())
                            {
                                QDomElement subElement = list.at(i).toElement();

                                QString subElementName =subElement.nodeName();

                                //name of the class
                                if(subElementName == "compoundDiatomsname")
                                {
                                    m_className = subElement.text();
                                }
                                else if(subElementName == "basecompoundref")
                                {
                                    m_baseClasses.push_back(loadInheritence(subElement));
                                }
                                else if(subElementName == "derivedcompoundref")
                                {
                                    m_derivedClasses.push_back(loadInheritence(subElement));
                                }
                                else if(subElementName == "includes")
                                {
                                    RefItem item;
                                    item.name = subElement.text();
                                    item.refId = subElement.attribute("refid");

                                    if(subElement.attribute("local") == "yes")
                                    {
                                        m_localIncludes.push_back(item);
                                    }
                                    else
                                    {
                                        m_externalIncludes.push_back(item);
                                    }
                                }
                                else if(subElementName == "briefdescription") //@todo add more functionality here
                                {
                                    DoxyDescription description;
                                    description.loadConfig(subElement);
                                    m_briefDescription = description.getText();
                                }
                                else if(subElementName == "detaileddescription")
                                {
                                    DoxyDescription description;
                                    description.loadConfig(subElement);
                                    m_detailedDescription = description.getText();
                                }
                                else if(subElementName == "location")
                                {
                                    m_descriptionLocation = subElement.attribute("file");
                                    m_definitionLocation = subElement.attribute("bodyfile");
                                }
                                else if(subElementName == "sectiondef")
                                {
                                    loadSectionDef(subElement);
                                }
                            }
                        }
                    }
                 }
               root = root.nextSibling();
            }
        }
    }
}

QString DoxyClass::getClassDescription() const
{
}

QString DoxyClass::getClassName() const
{
    return m_className;
}



/************************************************ Private *****************************/

/**
 * @brief DoxyClass Loads any inherited item from config
 * @param element The element to load the inheritence from
 */
Inheritance DoxyClass::loadInheritence(const QDomElement &element)
{
    Inheritance inherited;

    inherited.classRefrence.refId = element.attribute("refid");
    inherited.classRefrence.name = element.text();
    inherited.protection = Enumerators::ProtectionType::protectionFromString(element.attribute("prot"));
    inherited.virt = Enumerators::VirtualType::virtualFromString(element.attribute("virt"));

    return inherited;
}

IDoxyMemeberDef *DoxyClass::loadSectionDef(const QDomElement &element)
{
    QString type = element.attribute("kind");

    QDomNodeList list = element.childNodes();

    for(int i =0; i < list.size(); i++)
    {
        if(list.at(i).isElement())
        {
            QDomElement subElement = list.at(i).toElement();

            if(subElement.tagName() == "memberdef")
            {
                DoxMemberDef* def = new DoxMemberDef();
                def->loadConfig(subElement);
            }
            else
            {
                qDebug()  << "What is this ?? " << subElement.tagName();
            }
        }
    }


//    //then perfroma switch absed on what was loaded
//    switch (kind)lementName
//    {
//    case USER_DEFINED:

//        break;

//    case PUBLIC_TYPE:

//        break;

//    case PUBLIC_FUNC:

//        break;

//    case PUBLIC_ATTRIB:

//        break;

//    case PUBLIC_SLOT:

//        break;

//    case SIGNAL_T:

//        break;

//    case DCOP_FUNC:

//        break;

//    case PROPERTY:

//        break;

//    case EVENT:

//        break;

//    case PUBLIC_STATIC_FUNC:

//        break;

//    case PUBLIC_STATIC_ATTRIB:

//        break;

//    case PROTECTED_TYPE:

//        break;

//    case PROTECTED_FUNC:

//        break;

//    case PROTECTED_ATTRIB:

//        break;

//    case PROTECTED_SLOT:

//        break;

//    case PROTECTED_STATIC_FUNC:

//        break;

//    case PROTECTED_STATIC_ATTRIB:

//        break;

//    case PACKAGE_TYPE:

//        break;

//    case PACKAGE_FUNC:

//        break;

//    case PACKAGE_ATTRIB:

//        break;

//    case PACKAGE_STATIC_FUNC:

//        break;

//    case PACKAGE_STATIC_ATTRIB:

//        break;

//    case PRIVATE_TYPE:

//        break;

//    case PRIVATE_FUNC:

//        break;

//    case PRIVATE_ATTRIB:

//        break;

//    case PRIVATE_SLOT:

//        break;

//    case PRIVATE_STATIC_FUNC:

//        break;

//    case PRIVATE_STATIC_ATTRIB:

//        break;

//    case FRIEND:

//        break;

//    case RELATED:

//        break;

//    case DEFINE:

//        break;

//    case PROTOTYPE:

//        break;

//    case TYPEDEF:

//        break;

//    case ENUM:

//        break;

//    case FUNC:

//        break;

//    case VAR:

//        break;


//    default:
//        //PUBLIC_FUNC:
//        break;
//    }
}


